﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace PaiBanDaPing
{
    /// <summary>
    /// DisplayScreenCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class CustomScreenCtrl : UserControl
    {

        XDocument doc = new XDocument();
        string autoupdateFile = "autoupdate.xml";

        public CustomScreenCtrl()
        {
            InitializeComponent();
            InitXML();
            this.Loaded += CustomScreenCtrl_Loaded;
        }

        private void CustomScreenCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void InitXML()
        {
            if (File.Exists(autoupdateFile))
            {
                try
                {
                    doc = XDocument.Load(autoupdateFile);
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.Message);
                }
            }
            else
                System.Windows.MessageBox.Show(autoupdateFile + "不存在");
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CustomScreenModel sCreen = this.DataContext as CustomScreenModel;
                ScreenManger.DisplayWindows[sCreen.DisplayScreen.DeviceName].DisPlay(sCreen);
                doc.Root.Element("DaPing").Elements("Url").ElementAt(sCreen.index).SetValue(sCreen.Url);
                doc.Save(autoupdateFile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
